//
//  YMQRCodeManager.m
//  QrCode
//
//  Created by Andrew on 2018/6/22.
//  Copyright © 2018年 Andrew. All rights reserved.
//

#import "YMQRCodeManager.h"
#import <Photos/Photos.h>
#import <MBProgressHUD+JDragon.h>

@implementation YMQRCodeManager

/** 校验是否有相机权限 */
+ (void)ym_checkCameraAuthorizationStatusWithGrand:(void (^)(BOOL))permissionGrandted
{
    AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    switch (videoAuthStatus) {
        case AVAuthorizationStatusAuthorized://已授权
        {
            permissionGrandted(YES);
        }
            break;
            
        case AVAuthorizationStatusNotDetermined://未询问用户是否授权
        {
            //提示用户授权
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                permissionGrandted(granted);
            }];
        }
            
        case AVAuthorizationStatusRestricted://用户拒绝授权或权限受限
            break;
            
        case AVAuthorizationStatusDenied:
        {
            [MBProgressHUD showTipMessageInView:@"请在”设置-隐私-相机”选项中，允许访问你的相机"];
        }
            break;
        default:
            break;
    }
}

/** 校验是否有相册权限 */
+ (void)ym_checkAlbumAuthorizationStatusWithGrand:(void (^)(BOOL))permissionGrandted
{
    PHAuthorizationStatus photoAuthStatus = [PHPhotoLibrary authorizationStatus];
    switch (photoAuthStatus) {
        case PHAuthorizationStatusAuthorized://已授权
        {
            permissionGrandted(YES);
        }
            break;
        
        case PHAuthorizationStatusNotDetermined://未询问用户是否授权
        {
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                permissionGrandted(status == PHAuthorizationStatusAuthorized);
            }];
        }
            break;
            
        case PHAuthorizationStatusRestricted://用户拒绝授权或授权受限
            break;
            
        case PHAuthorizationStatusDenied:
        {
            [MBProgressHUD showTipMessageInView:@"请在”设置-隐私-相片”选项中，允许访问你的相册"];
        }
            break;
        default:
            break;
    }
}

/** 根据扫描类型配置支持编码格式 */
+ (NSArray *)ym_metadataObjectTypesWithType:(YMScannerType)scannerType
{
    switch (scannerType) {
        case YMScannerTypeQRCode:
            {
                return @[AVMetadataObjectTypeQRCode];
            }
            break;
            
        case YMScannerTypeBarCode:
        {
            return @[AVMetadataObjectTypeEAN13Code,
                     AVMetadataObjectTypeEAN8Code,
                     AVMetadataObjectTypeUPCECode,
                     AVMetadataObjectTypeCode39Code,
                     AVMetadataObjectTypeCode39Mod43Code,
                     AVMetadataObjectTypeCode93Code,
                     AVMetadataObjectTypeCode128Code,
                     AVMetadataObjectTypePDF417Code];
        }
            break;
            
        case YMScannerTypeBoth:
        {
            return @[AVMetadataObjectTypeQRCode,
                     AVMetadataObjectTypeEAN13Code,
                     AVMetadataObjectTypeEAN8Code,
                     AVMetadataObjectTypeUPCECode,
                     AVMetadataObjectTypeCode39Code,
                     AVMetadataObjectTypeCode39Mod43Code,
                     AVMetadataObjectTypeCode93Code,
                     AVMetadataObjectTypeCode128Code,
                     AVMetadataObjectTypePDF417Code];
        }
            break;
            
        default:
            break;
    }
}

/** 根据扫描器类型配置导航栏标题 */
+ (NSString *)ym_navigationItemTitleWithType:(YMScannerType)scannerType
{
    switch (scannerType) {
        case YMScannerTypeQRCode:
        {
            return @"二维码";
        }
            break;
            
        case YMScannerTypeBarCode:
        {
            return @"条码";
        }
            break;
            
        case YMScannerTypeBoth:
        {
            return @"二维码/条码";
        }
            break;
        default:
            break;
    }
}

/** 手电筒开关 */
+ (void)ym_flashlightOn:(BOOL)on
{
    AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if ([captureDevice hasTorch] && [captureDevice hasFlash]) {
        [captureDevice lockForConfiguration:nil];
        if (on) {
            [captureDevice setTorchMode:AVCaptureTorchModeOn];
            [captureDevice setFlashMode:AVCaptureFlashModeOn];
        } else {
            [captureDevice setTorchMode:AVCaptureTorchModeOff];
            [captureDevice setFlashMode:AVCaptureFlashModeOff];
        }
        [captureDevice unlockForConfiguration];
    }
}

@end
